package com.ftg.learn.controller;

import com.ftg.learn.domain.SysMenu;
import com.ftg.learn.domain.SysUserDomain;
import com.ftg.learn.service.SysUserService;
import com.ftg.learn.util.MenuVo;
import com.ftg.learn.util.PageEx;
import com.ftg.learn.util.ToWeb;
import com.ftg.learn.vo.UserListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author kangjx
 * @date 2024/1/31
 */
@RestController
@RequestMapping("/user")
@Api(tags="用户管理")
public class SysUserController {

    @Resource
    SysUserService sysUserService;


    @ApiOperation("登陆功能")
    @PostMapping("/login")
    public ToWeb<SysUserDomain> login(
            @ApiParam(value = "account") @RequestParam(value = "account")String account,
            @ApiParam(value = "password") @RequestParam(value = "password")String password
    ){
        return sysUserService.login(account,password);
    }

    @ApiOperation("登陆成功以后权限")
    @GetMapping("/auth")
    public ToWeb<List<MenuVo>> auth(
            @ApiParam(value = "userId") @RequestParam(value = "userId")Integer userId
    ){
        return sysUserService.auth(userId);
    }

    @GetMapping("/list")
    @ApiOperation("列表功能")
    public ToWeb<PageEx<UserListVo>> list(
            @ApiParam(value = "name") @RequestParam(value = "name",required = false) String name,
            @ApiParam(value = "phone") @RequestParam(value = "phone",required = false)String phone,
            @ApiParam(value = "status")@RequestParam(value = "status",required = false)Integer status,
            @ApiParam(value = "pageNo") @RequestParam(value = "pageNo",required = false,defaultValue = "1")long pageNo,
            @ApiParam(value = "pageSize") @RequestParam(value = "pageSize",required = false,defaultValue = "10")long pageSize
    ){
        System.out.println(pageSize);
        return sysUserService.list(name, phone, status, pageNo, pageSize);
    }


    @PostMapping("/insert")
    public ToWeb<Integer> insert(
            @RequestBody SysUserDomain domain,
            @ApiParam(value = "roleids") @RequestParam(value = "roleids")Integer[] roleids
    ){
        return sysUserService.insert(domain,roleids);
    }


    @DeleteMapping("/delete/{id}")
    public ToWeb<Integer> deleteById(
            @PathVariable("id") Integer id
    ){
        return sysUserService.deleteById(id);
    }

    @GetMapping("/findone/{id}")
    public ToWeb<Map<String,Object>> findone(
            @ApiParam(value = "id") @PathVariable("id") Integer id
    ){
        return sysUserService.findone(id);
    }

    @PutMapping("/update")
    public ToWeb<Integer> update(
           @RequestBody SysUserDomain domain,
           @ApiParam(value = "roleids")@RequestParam("roleids") Integer[] roleIds
    ){
        return sysUserService.update(domain,roleIds);
    }

}
